package io.github.fb513.api.config;

import com.auth0.jwt.exceptions.JWTVerificationException;
import io.github.fb513.api.enums.RespMetaEnum;
import io.github.fb513.api.exception.WebBaseException;
import io.github.fb513.api.model.CommonResp;
import io.github.fb513.api.util.RespUtils;
import io.micrometer.core.instrument.config.validate.ValidationException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public CommonResp<?> missingServletRequestParameterException(HttpServletRequest request, MissingServletRequestParameterException e) {
        log.warn(" ====== Request url is [{}] ====== ", request.getRequestURL(), e);
        return RespUtils.warning(RespMetaEnum.PARAM_ERROR);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResp<?> methodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
        log.warn(" ====== Request url is [{}] ====== ", request.getRequestURL(), e);
        String errMsg = "参数错误";
        FieldError fieldError = e.getBindingResult().getFieldError();
        if (fieldError != null && StringUtils.hasLength(fieldError.getDefaultMessage())) {
            errMsg = fieldError.getField() + fieldError.getDefaultMessage();
        }
        return RespUtils.warning(400, errMsg);
    }

    @ExceptionHandler(ValidationException.class)
    public CommonResp<?> validationException(HttpServletRequest request, ValidationException e) {
        log.warn(" ====== Request url is [{}] ====== ", request.getRequestURL(), e);
        return RespUtils.warning(RespMetaEnum.PARAM_ERROR);
    }

    @ExceptionHandler(JWTVerificationException.class)
    public CommonResp<?> tokenExpiredException(HttpServletRequest request, JWTVerificationException e) {
        log.warn(" ====== Request url is [{}] ====== ", request.getRequestURL(), e);
        return RespUtils.warning(RespMetaEnum.NOT_LOGIN);
    }

    @ExceptionHandler(WebBaseException.class)
    public CommonResp<?> webBaseException(HttpServletRequest request, WebBaseException e) {
        log.warn(" ====== Request url is [{}] ====== ", request.getRequestURL(), e);
        return RespUtils.warning(e.getStatus(), e.getMsg());
    }

    @ExceptionHandler(Throwable.class)
    public CommonResp<?> defaultErrorView(HttpServletRequest request, Throwable e) {
        log.warn(" ====== Request url is [{}] ====== ", request.getRequestURL(), e);
        return RespUtils.warning(RespMetaEnum.TIMEOUT);
    }
}
